//
//  YZMFundDetailCell.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/4/12.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMFundDetailCell.h"
#import <NSDate+DateTools.h>
#import "NSNumber+Utils.h"
#import <NSDate+DateTools.h>
#import "YZMAlertView.h"

@interface YZMFundDetailCell ()

@property (nonatomic, strong) UIView *listView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *stateLabel;
@property (nonatomic, strong) UIView *detailView;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UIView *withDrawlView;
@property (nonatomic, strong) UIView *progressView;
@property (nonatomic, strong) UIImageView *wdImageView;
@property (nonatomic, strong) UIImageView *progressImageView;
@property (nonatomic, strong) UIImageView *successImageView;
@property (nonatomic, strong) UIImageView *failedImageView;
@property (nonatomic, strong) UIView *firlineView;
@property (nonatomic, strong) UIView *seclineView;
@property (nonatomic, strong) UIView *thilineView;
@property (nonatomic, strong) UIView *foulineView;
@property (nonatomic, strong) UILabel *wdLabel;
@property (nonatomic, strong) UILabel *progressLabel;
@property (nonatomic, strong) UILabel *successLabel;
@property (nonatomic, strong) UILabel *wdTimeLabel;
@property (nonatomic, strong) UILabel *progressTimeLabel;
@property (nonatomic, strong) UILabel *successTimeLabel;
@property (nonatomic, strong) UILabel *wdAccountLabel;
@property (nonatomic, strong) UILabel *wdBankLabel;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UILabel *detailLB;

@end

@implementation YZMFundDetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createObjects];
        self.leftMargin = 0.0;
        self.rightMargin = 0.0;
    }
    return self;
}

- (void)createObjects {
    WS(ws)
    
    self.listView = [[UIView alloc] init];
    [self.contentView addSubview:self.listView];
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(ws.contentView);
        make.top.equalTo(ws.contentView).with.offset(0);
        make.height.mas_equalTo(70);
    }];
    
    self.titleLabel = [YZMAppStyle labelWithMediumWhite];
    [self.listView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(ws.listView).with.offset(ControlSpacing15);
    }];
    
    self.timeLabel = [YZMAppStyle labelWithSmallLightGray];
    [self.listView addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.listView).with.offset(ControlSpacing15);
        make.bottom.equalTo(ws.listView).with.offset(-ControlSpacing15);
    }];
    
    self.priceLabel = [YZMAppStyle labelWithMediumWhite];
    [self.listView addSubview:self.priceLabel];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.listView).with.offset(ControlSpacing15);
        make.right.equalTo(ws.listView).with.offset(-ControlSpacing15);
    }];
    
    self.stateLabel = [YZMAppStyle labelWithMediumLightGray];
    [self.listView addSubview:self.stateLabel];
    [self.stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(ws.listView).with.offset(-ControlSpacing15);
        make.right.equalTo(ws.listView).with.offset(-ControlSpacing15);
    }];
    
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelBtn.hidden = YES;
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:[YZMAppStyle buttonSelectedColor] forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = [YZMAppStyle fontWithSize:12];
    self.cancelBtn.layer.cornerRadius = 2;
    self.cancelBtn.layer.borderWidth = 1;
    self.cancelBtn.layer.borderColor = [YZMAppStyle buttonSelectedColor].CGColor;
    [self.cancelBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.listView addSubview:self.cancelBtn];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(ws.listView).with.offset(-8);
        make.right.equalTo(ws.listView).with.offset(-15);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(22);
    }];
    
    self.detailView = [[UIView alloc] init];
    self.detailView.hidden = YES;
    self.detailView.backgroundColor = [YZMAppStyle appBackgroundColor];
    [self.contentView addSubview:self.detailView];
    [self.detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(ws.contentView);
        make.top.equalTo(ws.listView.mas_bottom);
    }];
    
    UIView *splitLine = [YZMAppStyle splitLine];
    [self.detailView addSubview:splitLine];
    [splitLine mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.right.equalTo(ws.detailView);
        make.top.equalTo(ws.detailView.mas_top);
    }];
    
    self.detailLabel = [YZMAppStyle labelWithSmallGray];
    //文字居中显示
    self.detailLabel.textAlignment = NSTextAlignmentLeft;
    //自动折行设置
    self.detailLabel.numberOfLines = 0;
    [self.detailView addSubview:self.detailLabel];
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(splitLine.mas_bottom).with.offset(10);
        make.left.equalTo(ws.detailView).with.offset(ControlSpacing15);
        make.right.equalTo(ws.detailView).with.offset(-ControlSpacing15);
        make.bottom.equalTo(ws.detailView.mas_bottom).with.offset(-10);
    }];
    
    //提现明细
    self.withDrawlView = [[UIView alloc] init];
    self.withDrawlView.hidden = YES;
    self.withDrawlView.backgroundColor = [YZMAppStyle appBackgroundColor];
    [self.contentView addSubview:self.withDrawlView];
    [self.withDrawlView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(ws.contentView);
        make.top.equalTo(ws.listView.mas_bottom);
        make.height.mas_equalTo(137);
    }];
    
    UIView *splitLineTwo = [YZMAppStyle splitLine];
    [self.withDrawlView addSubview:splitLineTwo];
    [splitLineTwo mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.right.equalTo(ws.withDrawlView);
        make.top.equalTo(ws.withDrawlView.mas_top);
    }];
    
    self.progressView = [[UIView alloc] init];
    [self.withDrawlView addSubview:self.progressView];
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.right.equalTo(ws.withDrawlView);
        make.top.equalTo(splitLineTwo.mas_bottom).with.offset(10);
        make.height.mas_equalTo(40);
    }];
    
    self.seclineView = [YZMAppStyle grayUnableLine];
    [self.progressView addSubview:self.seclineView];
    [self.seclineView mas_makeConstraints:^(MASConstraintMaker *make){
        make.right.equalTo(ws.progressView.mas_centerX);
        make.width.equalTo(ws.progressView).dividedBy(4).offset(-25);
        make.centerY.equalTo(ws.progressView);
    }];

    self.firlineView = [YZMAppStyle grayUnableLine];
    self.firlineView.backgroundColor= [YZMAppStyle colorForBlueText];
    [self.progressView addSubview:self.firlineView];
    [self.firlineView mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.width.equalTo(ws.seclineView);
        make.right.equalTo(ws.seclineView.mas_left);
    }];

    self.thilineView = [YZMAppStyle grayUnableLine];
    [self.progressView addSubview:self.thilineView];
    [self.thilineView mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.width.equalTo(ws.seclineView);
        make.left.equalTo(ws.seclineView.mas_right);
    }];

    self.foulineView = [YZMAppStyle grayUnableLine];
    [self.progressView addSubview:self.foulineView];
    [self.foulineView mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.width.equalTo(ws.seclineView);
        make.left.equalTo(ws.thilineView.mas_right);
    }];
    
    self.wdImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_withDraw"]];
    [self.progressView addSubview:self.wdImageView];
    [self.wdImageView mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.equalTo(ws.progressView).with.offset(38);
        make.centerY.equalTo(ws.progressView);
    }];

    self.progressImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_ progress_gray"] highlightedImage:[UIImage imageNamed:@"icon_ progress"]];
    [self.progressView addSubview:self.progressImageView];
    [self.progressImageView mas_makeConstraints:^(MASConstraintMaker *make){
        make.center.equalTo(ws.progressView);
    }];
    
    self.successImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_success_gray"] highlightedImage:[UIImage imageNamed:@"icon_success"]];
    [self.progressView addSubview:self.successImageView];
    [self.successImageView mas_makeConstraints:^(MASConstraintMaker *make){
        make.right.equalTo(ws.progressView).with.offset(-38);
        make.centerY.equalTo(ws.progressView);
    }];
    
    self.failedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_failed_gray"] highlightedImage:[UIImage imageNamed:@"icon_failed"]];
    self.failedImageView.hidden = YES;
    [self.progressView addSubview:self.failedImageView];
    [self.failedImageView mas_makeConstraints:^(MASConstraintMaker *make){
        make.right.equalTo(ws.progressView).with.offset(-38);
        make.centerY.equalTo(ws.progressView);
    }];

    self.wdLabel = [YZMAppStyle labelWithSmallWhite];
    [self.withDrawlView addSubview:self.wdLabel];
    [self.wdLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(ws.progressView.mas_bottom);
        make.centerX.equalTo(ws.wdImageView);
    }];
    
    self.progressLabel = [YZMAppStyle labelWithSmallWhite];
    [self.withDrawlView addSubview:self.progressLabel];
    [self.progressLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(ws.progressView.mas_bottom);
        make.centerX.equalTo(ws.progressImageView);
    }];
    
    self.successLabel = [YZMAppStyle labelWithSmallWhite];
    [self.withDrawlView addSubview:self.successLabel];
    [self.successLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(ws.progressView.mas_bottom);
        make.centerX.equalTo(ws.successImageView);
    }];
    
    self.wdTimeLabel = [YZMAppStyle labelWithSmallLightGray];
    self.wdTimeLabel.font = [UIFont systemFontOfSize:10];
    [self.withDrawlView addSubview:self.wdTimeLabel];
    [self.wdTimeLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(ws.wdLabel.mas_bottom).with.offset(5);
        make.centerX.equalTo(ws.wdLabel);
    }];

    self.progressTimeLabel = [YZMAppStyle labelWithSmallLightGray];
    self.progressTimeLabel.font = [UIFont systemFontOfSize:10];
    [self.withDrawlView addSubview:self.progressTimeLabel];
    [self.progressTimeLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(ws.progressLabel.mas_bottom).with.offset(5);
        make.centerX.equalTo(ws.progressLabel);
    }];
    
    self.successTimeLabel = [YZMAppStyle labelWithSmallLightGray];
    self.successTimeLabel.font = [UIFont systemFontOfSize:10];
    [self.withDrawlView addSubview:self.successTimeLabel];
    [self.successTimeLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(ws.progressLabel.mas_bottom).with.offset(5);
        make.centerX.equalTo(ws.successLabel);
    }];
    
    self.wdAccountLabel = [YZMAppStyle labelWithMediumLightGray];
    self.wdAccountLabel.text = @"提现账户:";
    [self.withDrawlView addSubview:self.wdAccountLabel];
    [self.wdAccountLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(ws.wdTimeLabel.mas_bottom).with.offset(15);
        make.left.equalTo(ws.withDrawlView).with.offset(20);
    }];
    
    self.wdBankLabel = [YZMAppStyle labelWithMediumLightGray];
    [self.withDrawlView addSubview:self.wdBankLabel];
    [self.wdBankLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(ws.wdAccountLabel);
        make.left.equalTo(ws.wdAccountLabel.mas_right);
    }];
    
    self.detailLB = [YZMAppStyle labelWithSmallGray];
    self.detailLB.hidden = YES;
    self.detailLB.text = @"详情:您已取消提现,提现金额已返还至你的账户余额。";
    self.detailLB.numberOfLines = 0;
    [self.withDrawlView addSubview:self.detailLB];
    [self.detailLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(ws.wdAccountLabel);
        make.top.equalTo(ws.wdBankLabel.mas_bottom).offset(10);
    }];
}

- (void)showContentView:(YZMFundListModel *)item fundType:(YZS_FUND_TYPE)type {
    
    self.titleLabel.text = item.explain;
    self.stateLabel.text = [item statusName];
    self.stateLabel.textColor = [item statusColor];
    self.stateLabel.hidden = NO;
    self.cancelBtn.hidden = YES;
    self.detailLB.hidden = YES;
    [self.withDrawlView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(137);
    }];

    if (type == YZS_FUND_TYPE_FOR_RECHARGE || type == YZS_FUND_TYPE_FOR_WITHDRAW) {
        switch (item.status) {
            case 0:
                //待处理
                self.timeLabel.text = [item.requestTime formattedDateWithFormat:@"yyyy/MM/dd HH:mm:ss"];
                if (type == YZS_FUND_TYPE_FOR_WITHDRAW) {
                    
                    self.cancelBtn.hidden = NO;
                    self.stateLabel.hidden = YES;
                }
                
                break;
            case 1:
                //成功
            case 2:
                //失败
            case 3:
                //已取消
                if (type == YZS_FUND_TYPE_FOR_WITHDRAW) {
                    
                    self.detailLB.hidden = NO;
                    [self.withDrawlView mas_updateConstraints:^(MASConstraintMaker *make) {
                        
                        make.height.mas_equalTo(162);
                    }];
                }
            
            case 4:
                //处理中
            case 5:
                //汇款中
                self.timeLabel.text = [item.disposeTime formattedDateWithFormat:@"yyyy/MM/dd HH:mm:ss"];
                
                break;
            default:
                break;
        }
    } else {
        
        if (type == YZS_FUND_TYPE_FOR_ALL)
            self.timeLabel.text = [item.requestTime formattedDateWithFormat:@"yyyy/MM/dd HH:mm:ss"];
        else
            self.timeLabel.text = [item.time formattedDateWithFormat:@"yyyy/MM/dd HH:mm:ss"];
    }
    
    if (item.type == FUND_FLOW_FOR_IN) {
        
        self.priceLabel.text = [@"+" stringByAppendingString:@(item.money).decimalP02fString];
    } else {
        
        self.priceLabel.text = [@"-" stringByAppendingString:@(item.money).decimalP02fString];
    }
}

- (void)showDetailView {
    self.detailView.hidden = NO;
}

- (void)hiddeDetailView {
    self.detailView.hidden = YES;
}

- (void)showWithDrawView:(YZMFundListModel *)item {
    
    self.withDrawlView.hidden = NO;
    self.firlineView.backgroundColor= [YZMAppStyle colorForGrayUnable];
    self.seclineView.backgroundColor= [YZMAppStyle colorForGrayUnable];
    self.thilineView.backgroundColor= [YZMAppStyle colorForGrayUnable];
    self.foulineView.backgroundColor= [YZMAppStyle colorForGrayUnable];
    self.wdLabel.text = @"提现";
    self.progressLabel.text = @"处理中";
    self.successLabel.text = @"提现成功";
    self.wdTimeLabel.text = [item.requestTime formattedDateWithFormat:@"yyyy/MM/dd"];
    self.progressTimeLabel.text = [item.requestTime formattedDateWithFormat:@""];
    self.successTimeLabel.text = [item.requestTime formattedDateWithFormat:@""];
    self.wdImageView.highlighted = NO;
    self.progressImageView.highlighted = NO;
    self.successImageView.highlighted = NO;
    self.failedImageView.hidden = YES;
    
    switch (item.status) {
        case 0:
            self.firlineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            //待处理
            self.wdImageView.highlighted = YES;
            
            break;
        case 1:
            //成功
            self.firlineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            self.seclineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            self.thilineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            self.foulineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            
            self.wdImageView.highlighted = YES;
            self.progressImageView.highlighted = YES;
            self.successImageView.highlighted = YES;
            self.failedImageView.hidden = YES;
            
            self.progressTimeLabel.text = [item.disposeTime formattedDateWithFormat:@"yyyy/MM/dd"];
            self.successTimeLabel.text = [item.disposeTime formattedDateWithFormat:@"yyyy/MM/dd"];
            
            break;
        case 2:
            //失败
            self.firlineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            self.seclineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            self.thilineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            self.foulineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            
            self.wdImageView.highlighted = YES;
            self.progressImageView.highlighted = YES;
            self.failedImageView.highlighted = YES;
            self.failedImageView.hidden = NO;
            self.successLabel.text = @"提现失败";
            
            self.progressTimeLabel.text = [item.disposeTime formattedDateWithFormat:@"yyyy/MM/dd"];
            self.successTimeLabel.text = [item.disposeTime formattedDateWithFormat:@"yyyy/MM/dd"];
            
            break;
        case 3:
            //已取消
            self.firlineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            self.seclineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            self.thilineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            self.foulineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            
            self.wdImageView.highlighted = YES;
            self.progressImageView.highlighted = YES;
            self.failedImageView.highlighted = YES;
            self.failedImageView.hidden = NO;
            
            self.successLabel.text = @"已取消";
            
            self.progressTimeLabel.text = [item.disposeTime formattedDateWithFormat:@"yyyy/MM/dd"];
            self.successTimeLabel.text = [item.disposeTime formattedDateWithFormat:@"yyyy/MM/dd"];
            
            break;
        case 4:
            //处理中
            self.firlineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            self.seclineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            self.thilineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            
            self.wdImageView.highlighted = YES;
            self.progressImageView.highlighted = YES;
            
            self.progressTimeLabel.text = [item.disposeTime formattedDateWithFormat:@"yyyy/MM/dd"];
            
            break;
        case 5:
            //汇款中
            self.firlineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            self.seclineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            self.thilineView.backgroundColor= [YZMAppStyle colorForYellowButton];
            
            self.wdImageView.highlighted = YES;
            self.progressImageView.highlighted = YES;
            
            self.progressTimeLabel.text = [item.disposeTime formattedDateWithFormat:@"yyyy/MM/dd"];
            
            break;
        default:
            break;
    }

    self.wdBankLabel.text = item.detail;
}

- (void)hiddeWithDrawView {
    self.withDrawlView.hidden = YES;
}

- (void)setCellHeight:(CGFloat)cellHeight {

    if (!_cellHeight) {

        [self.detailLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.height.mas_equalTo(cellHeight);
        }];
        
    }
}

- (void)clickBtn:(UIButton *)sender {
    
    if (self.btnAction) {
        
        YZMAlertView *alertView = [[YZMAlertView alloc] init];
        alertView.contentLabel.text = @"取消提现后金额自动退还到账户余额";
        alertView.contentLabel.textAlignment = NSTextAlignmentCenter;
        alertView.confirmHandle = ^() {
            
            self.btnAction();
            return YES;
        };
        
        [alertView showInView:YZMCMInstance.rootTabBarCtrl.view];
    }
}

@end
